<?php

class UsersController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->view->ind = 'Список пользователей';
        $user = new Application_Model_User();
        $this->view->users = $user->getAllUsers();
    }

    public function registrationAction() {
        $this->view->register = 'Регистрация нового пользователя';
        $form = new Application_Model_Form_Registration();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $user = new Application_Model_User();
                $user->fill($form->getValues());
                $user->password = sha1($user->password);
                $user->created = date('Y-m-d H:i:s');
                $user->code = uniqid();
                $user->role = 'user';
                $user->save();
                $user->sendActivationEmail();
                $this->_redirect('users');
            }
        }
        $this->view->form = $form;
    }

    public function addAction() {
        $this->view->register = 'Добавление нового пользователя';
        $form = new Application_Model_Form_AddUser();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $user = new Application_Model_User();
                $user->fill($form->getValues());
                $user->password = sha1($user->password);
                $user->created = date('Y-m-d H-i-s');
                $user->role = 'user';
                $user->activated = 1;
                $user->save();
                $this->_redirect('index');
            }
        }

        $this->view->form = $form;
    }

    public function loginAction() {
        $this->view->login = 'Авторизация пользователя';
        $form = new Application_Model_Form_Login();
        if ($this->getRequest()->isPost()):
            if ($form->isValid($this->getRequest()->getPost())):
                $user = new Application_Model_User();
                if ($user->authorize($form->getValue('username'), $form->getValue('password'))):
                    if ($form->getValue('remember')):
                        Zend_Session::rememberMe(60 * 10);
                    endif;
                    $this->_redirect('/');
                else:
                    $this->view->error = 'Неверные данные авторизации';
                endif;
            endif;
        endif;

        $this->view->form = $form;
    }

    public function logoutAction() {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_redirect("/");
        //    var_dump($auth->getIdentity());
    }

    public function editAction() {
        if (Zend_Auth::getInstance()->getIdentity()->role == 'admin'):
            $id = $this->_getParam('id');
        if(!$id):
            $id = Zend_Auth::getInstance()->getIdentity()->id;
        endif;
        else:
            $id = Zend_Auth::getInstance()->getIdentity()->id;
        endif;

        $user = new Application_Model_User($id);
        $form = new Application_Model_Form_User($id);
        $form->populate($user->populateForm());
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $user->fill($form->getValues());
                $user->modified = date('Y-m-d H-i-s');
                $user->password = sha1($user->password);
                $user->save();
                $this->_redirect('users');
            }
        }
    }

    public function delAction() {
        $id = $this->_getParam('id');
        $user = new Application_Model_User($id);
        $user->delete();
        $this->_redirect('users');
    }

    public function viewAction() {
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $user = new Application_Model_User($id);
        $this->view->user = $user;
    }

    public function confirmAction() {
        $id = $this->_getParam('id');
        $code = $this->_getParam('code');
        $user = new Application_Model_User($id);
        if ($user->activated) {
            $this->view->message = 'Ваш аккаунт уже активирован';
        } else {
            if ($user->code === $code) {
                $user->activated = true;
                $user->save();
                $this->view->message = 'Ваш аккаунт успешно активирован';
            } else {
                $this->view->message = 'Неверные данные активации.';
            }
        }
    }

}

